import { createTitle, getTitles,delTitle,autoGenerate } from '../service';
import router from 'umi/router';
import { message } from 'antd';
import cookie from 'react-cookies';
import { userId } from '@/constant';

const creator = (type, payload) => ({ type, payload });
export default {
  namespace: 'title',
  state: {
    titles: [],
  },

  effects: {
    *createTitle({ payload }, { call, put }) {
      let res = yield call(createTitle, payload);
      if (res.httpStatus===200) {
        message.success(`${res.msg}`);
      }else{
        message.warn(`${res.msg}`); 
      }
      return res;
    },
    *getTitles(_, { call, put }) {
      let res = yield call(getTitles);
      if (res.httpStatus===200) {
          yield put(creator('save',{titles:res.data}))
      }else{
        message.warn(`${res.msg}`); 
      }
    },
    *delTitle({ payload }, { call, put }) {
      let res = yield call(delTitle, payload);
      return res;
    },
    *autoGenerate({ payload }, { call, put }) {
      let res = yield call(autoGenerate, payload);
      return res;
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  //监听路由
  // subscriptions: {
  //   setup({ dispatch, history }) {
  //     return history.listen(({ pathname, query }) => {
  //       if (pathname === '/users') {
  //         console.log(query,9999)
  //         dispatch({ type: 'fetch', payload: query });
  //       }
  //     })
  //   }
  // }
};
